package cn.stronglink.assetmanage.business.repair.order;

import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import cn.stronglink.assetmanage.BaseActivity;
import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.checkorder.CheckViewPagerAdapter;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.MessageEvent;
import cn.stronglink.assetmanage.entity.Repair;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.dao.CheckDataUtil;
import cn.stronglink.assetmanage.utils.dao.RepairDataUtil;

public class RepairOrderActivity extends BaseActivity {
    TabLayout tbRepair;
    ViewPager vpRepair;
    private RepairViewPagerAdapter mAdapter;
    private FragmentManager fm;
    private String[] mRepairTitle = MyApplication.APP_CONTEXT.getResources().
            getStringArray(R.array.repair);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_order);
        initView();
        initData();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    private void initView() {
        tbRepair = findViewById(R.id.tl_repair);
        vpRepair = findViewById(R.id.vp_repair);
    }

    private void initData() {
        fm = getSupportFragmentManager();
        List<Repair> mRepairList = RepairDataUtil.getDataByRepairedDistince();
        List<Repair> mUnRepairList = RepairDataUtil.getDataByunRepairDistince();
        int repairSize = mRepairList == null ? 0 : mRepairList.size();
        int unRepairSize = mUnRepairList == null ? 0 : mUnRepairList.size();
        mRepairTitle = new String[]{"待维修(" + unRepairSize + ")",
                "已维修(" + repairSize + ")"};
        mAdapter = new RepairViewPagerAdapter(fm, mRepairTitle);
        vpRepair.setAdapter(mAdapter);
        tbRepair.setupWithViewPager(vpRepair);
        tbRepair.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vpRepair.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        setSelectPosition(4);
        initData();
    }

    @Override
    public void onBackPressed() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateCheckData(MessageEvent messageEvent) {
        if (messageEvent != null && Constants.UPDATE_REPAIR_DATA.equals(messageEvent.getType())) {
            initData();
            setSelectPosition(4);
        }
    }
}
